package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.CouponCreateBo;
import com.cskaoyan.bean.bo.CouponDeleteBo;
import com.cskaoyan.bean.bo.CouponUpdateBo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 优惠券控制模块
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/4 15:10
 */
@RestController
@RequestMapping("admin/coupon")
public class CouponController {
    @Autowired
    CouponService couponService;
    /**
     * /admin/coupon/list?page=1&limit=20&sort=add_time&order=desc
     * 获取到客户端传递的参数-->BaseParam类;
     * @param baseParam
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo listAll( BaseParam baseParam ,String name,Integer type , Integer status) {
       CouponListVo couponListVO  =   couponService.listAllCoupon(baseParam, name , type,status);
        return BaseRespVo.ok(couponListVO);
    }

    /**
     *  /admin/coupon/create
     *
     *
     * 接收客户端参数
     * {
     *   "name": "我是优惠券 11",
     *   "desc": "介绍",
     *   "tag": "我是标签",
     *   "total": 0,
     *   "discount": "12",
     *   "min": "1",
     *   "limit": 1,
     *   "type": 0,
     *   "status": 0,
     *   "goodsType": 0,
     *   "goodsValue": [],
     *   "timeType": 0,
     *   "days": "1",
     *   "startTime": null,
     *   "endTime": null
     * }
     * 返回{
     *   "errno": 0,
     *   "data": {
     *     "id": 99,
     *     "name": "我是优惠券 11",
     *     "desc": "介绍",
     *     "tag": "我是标签",
     *     "total": 0,
     *     "discount": 12,
     *     "min": 1,
     *     "limit": 1,
     *     "type": 0,
     *     "status": 0,
     *     "goodsType": 0,
     *     "goodsValue": [],
     *     "timeType": 0,
     *     "days": 1,
     *     "addTime": "2022-06-04 16:26:17",
     *     "updateTime": "2022-06-04 16:26:17"
     *   },
     *   "errmsg": "成功"
     * }
     *
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody CouponCreateBo  couponCreateBo){
        // 复用CouponVo; 不然需要新建一个 CouponCreateVo;
     CouponVo couponCreateVo =      couponService.create(couponCreateBo);
        return  BaseRespVo.ok(couponCreateVo);
    }

    /**
     * GET /admin/coupon/read?id=93 ;
     * @param id
     * @return
     */
    @RequestMapping("read")
    public BaseRespVo read(Integer id){
          CouponReadVo couponReadVo =     couponService.read(id);
          return BaseRespVo.ok(couponReadVo);

    }

    /**
     * /admin/ad/update
     * POST+application;
     *
     * 复用了CouponUpdateBo; 但是应注意updateTime;
     */
    @RequestMapping("update")
    public BaseRespVo  update(@RequestBody CouponUpdateBo couponUpdateBo){
                CouponUpdateBo  couponUpdateVo  =  couponService.updateOneCoupon(couponUpdateBo);
        return BaseRespVo.ok(couponUpdateVo);
    }

    // @RequestMapping("delete")
    // public BaseRespVo delete(@RequestBody CouponDeleteBo couponDeleteBo ){
    //       couponService.deleteOneById(couponDeleteBo);
    //     return BaseRespVo.ok(null);
    // }

    /**
     * 2022年06月06日09:28:44 chy  修订: 假删除
     * @param couponDeleteBo
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody CouponDeleteBo couponDeleteBo ){
        couponService.deleteOneById(couponDeleteBo);
        return BaseRespVo.ok(null);
    }



    /**
     * /admin/coupon/listuser?page=1&limit=20&couponId=104&sort=add_time&order=desc
     * 混合接收,使用基本类型+引用类型; 顺序不对;
     * 所以使用基本类型和 String 接收参数;
     * @param
     * @param couponId
     * @return
     */
    @RequestMapping("listuser")
    public BaseRespVo  listuser( Integer page, Integer limit ,Integer couponId ,String sort, String order){
        BaseParam baseParam = new BaseParam();
        baseParam.setPage(page);
        baseParam.setLimit(limit);
        baseParam.setSort(sort);
        baseParam.setOrder(order);

        // System.out.println("baseParam = " + baseParam);
        // System.out.println("couponId = " + couponId);
        CouponListUserVo couponListUserVo =     couponService.listuserByCouponId(baseParam,couponId);
        TVO<CouponListUserVo> couponListUserVoTVO = new TVO<>();
        /**
         * 将获取到的list, 如何做分页?
         */
        return BaseRespVo.ok(couponListUserVoTVO);
    }



}

